package com.jinsilu.freight.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ObjectUtils;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jinsilu.freight.common.MyVBActivity;
import com.jinsilu.freight.databinding.ActivityManageAccountBinding;
import com.jinsilu.freight.helper.ActivityStackManager;
import com.jinsilu.freight.helper.CommonHelper;
import com.jinsilu.freight.helper.ReplaceHelper;
import com.jinsilu.freight.http.model.HttpData;
import com.jinsilu.freight.http.request.AccountCancellationApi;
import com.jinsilu.freight.http.request.LogoutApi;
import com.jinsilu.freight.http.response.UserInfoBean;
import com.jinsilu.freight.other.MTLConstants;
import com.jinsilu.freight.ui.dialog.MessageDialog;

/**
 * 账号管理
 *
 * @author yangyang
 * @time 2020-11-19 18:33
 */
public class ManageAccountActivity extends MyVBActivity<ActivityManageAccountBinding> {

    @Override
    protected void initView() {
        setOnClickListener(binding.layoutLoginOut, binding.tvResetPhone, binding.tvPrivacyAgreement, binding.tvAccountCancellation, binding.tvUsrAgreement);
    }

    @Override
    protected void initData() {
        UserInfoBean userInfoBean = (UserInfoBean) CommonHelper.getSavedObject("userInfo", UserInfoBean.class);
        if (!ObjectUtils.isEmpty(userInfoBean))
            binding.mePhone.setText(ReplaceHelper.phoneFormat(userInfoBean.getPhone()));
        else
            binding.mePhone.setText("");
    }

    @Override
    public void onClick(View v) {
        if (v == binding.layoutLoginOut) {
            loginOut();
        } else if (v == binding.tvResetPhone) {
            startActivityForResult(PhoneResetActivity.class, (resultCode, data) -> {
                if (resultCode == RESULT_OK && null != data) {
                    String newPhone = data.getStringExtra("phone");
                    binding.mePhone.setText(TextUtils.isEmpty(newPhone) ? "" : ReplaceHelper.phoneFormat(newPhone));
                }
            });
        } else if (v == binding.tvPrivacyAgreement) {
            BrowserActivity.start(this, MTLConstants.USER_PRIVACY_AGREEMENT);
        } else if (v == binding.tvUsrAgreement) {
            BrowserActivity.start(this, MTLConstants.USER_SERVICE_AGREEMENT);
        } else if (v == binding.tvAccountCancellation) {
            new MessageDialog.Builder(this)
                    .setTitle("账号注销")
                    .setMessage("您正在进行账号注销操作，点击确定后您账号的所有信息将被清空，请您再次确认")
                    .setListener(dialog -> accountCancellation())
                    .show();
        }
    }

    /**
     * 账号注销
     */
    private void accountCancellation() {
        // 账号注销
        EasyHttp.delete(this)
                .api(new AccountCancellationApi())
                .request(new HttpCallback<HttpData<Void>>(this) {

                    @Override
                    public void onSucceed(HttpData<Void> data) {
                        startActivity(LoginActivity.class);
                        // 进行内存优化，销毁除登录页之外的所有界面
                        CommonHelper.clearSPData(); // 清除缓存数据
                        CommonHelper.clearObj("userInfo");
                        CommonHelper.clearObj("sdk");
                        ActivityStackManager.getInstance().finishAllActivities(LoginActivity.class);
                    }
                });
    }

    /**
     * 退出登录
     */
    private void loginOut() {
        // 退出登录
        EasyHttp.delete(this)
                .api(new LogoutApi())
                .request(new HttpCallback<HttpData<Void>>(this) {

                    @Override
                    public void onSucceed(HttpData<Void> data) {
                        if (null != CommonHelper.getUserInfo() && !TextUtils.isEmpty(CommonHelper.getUserInfo().getPhone())) {
                            LoginActivity.start(ManageAccountActivity.this, CommonHelper.getUserInfo().getPhone(), "");
                        } else {
                            startActivity(LoginActivity.class);
                        }
                        // 进行内存优化，销毁除登录页之外的所有界面
                        CommonHelper.clearSPData(); // 清除缓存数据
                        ActivityStackManager.getInstance().finishAllActivities(LoginActivity.class);
                    }
                });
    }
}
